home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MacQForth 1.0 / documentation / Lessons / LESSON 0 < prev    next >
Encoding:
Text File  |  1995-03-23  |  10.5 KB  |  152 lines  |  [TEXT/ALFA]

  1.  
  2. LESSON 0                 "What Forth is and is not"
  3. --------------------------------------------------------------------------------
  4.  
  5.    Contents of this "lesson":
  6.    
  7.                1.  The Forth Paradigm
  8.                2.  Forth in relation to other programming languages
  9.                3.  Why use it?
  10.                
  11. ================================================================================
  12.  
  13.    This "lesson" attempts (note the word "attempts") to give the reader a 
  14.    sense of what Forth is, what it is not, and why one would bother to 
  15.    learn and use it.  The real lessons start in the next file.
  16.    
  17.    
  18.  
  19. The Forth Paradigm:   "What Forth is"
  20. -------------------------------------
  21.  
  22.    Forth was created to allow programmers the freedom to program as they 
  23.    see fit.  It is built of four basic parts: the Dictionary, the Stack, 
  24.    the Return Stack, and the Interpreter/Compiler.
  25.    
  26.    When programming in Forth the key elements are the Dictionary and the 
  27.    Stack.  The stack is the dynamic element of Forth, all activity takes 
  28.    place on it during program execution.  Active things act on something, 
  29.    and the object and source of the stack activity in Forth is the 
  30.    dictionary.  The dictionary is static and does not change during 
  31.    program execution (except for data written to reserved spaces) but is 
  32.    defined prior to execution.  The dictionary contains all the program code 
  33.    and data.  While there are variations in this arrangement, it is true in 
  34.    general:
  35.    
  36.    
  37.        +---------------------+
  38.        |                     |
  39.        |                     |
  40.        |                     |                +---+
  41.        |     The             |                |   |   <- The Stack
  42.        |                     |                +---+      (data manipulation)
  43.        |       Dictionary    |                |   |
  44.        |                     |                +---+
  45.        |  (the code)         |                |   |
  46.        |                     |                +---+
  47.        +---------------------+
  48.        
  49.        
  50.    Typically, the programmer creates the program source code and tests it 
  51.    by loading it into the interpreter/compiler.  This is often done word 
  52.    by word or screen by screen if using blocks.  Once all the code is 
  53.    written and tested many Forths allow the programmer to create a 
  54.    standalone application (Macintosh terminology) by loading the code, 
  55.    selecting a startup word and sealing off the rest of the system from 
  56.    the user.  This sealed system will appear as any other binary to the 
  57.    user.
  58.    
  59.    Again, a Forth program is built of words, which often use other words 
  60.    and sometimes make use of fixed data, all located within the 
  61.    dictionary.  An application is created by defining a "highest level" 
  62.    word, the word that runs the entire program, like main() in C, or the 
  63.    outermost BEGIN .. END. in Pascal.  Note that MacQForth cannot create 
  64.    standalone Macintosh applications but can save a memory image with a 
  65.    startup word specified so that it will run when loaded.
  66.    
  67.    Think of it this way.  In order to solve a particular problem the 
  68.    programmer builds a dictionary.  The dictionary contains all the words 
  69.    that are needed to express the solution to the problem, to speak the 
  70.    language of the problem.  These words are put together to "speak" the 
  71.    solution.  The advantage, of course, is that while every problem is 
  72.    different, many problems share some of the same words in their 
  73.    "spoken" solution.  Therefore, Forth is highly reusable, contrary to 
  74.    the charge of some critics that it is not.  Factoring, a Forth term 
  75.    that means removing discrete elements from a word that could be used 
  76.    elsewhere, makes this reusablility possible.  What level of factoring 
  77.    is good is a matter of some debate.
  78.    
  79.    Many older Forths, and some newer ones too, go beyond the idea of 
  80.    programming language and become an entire programming 
  81.    language/operating system combination.  It becomes a means for 
  82.    controlling the entire computer.  Instead of a compiler or interpreter 
  83.    that runs programs created in a separate editor using files on disk 
  84.    mantained by an operating system, these Forths _are_ the compiler, 
  85.    _are_ the interpreter, _are_ the editor, and _are_ the operating 
  86.    system.  There is no distinction, and no overhead.  These are the 
  87.    block-based Forths and might be worth investigating after learning 
  88.    with MacQForth.
  89.    
  90.    
  91.    
  92.    
  93. Forth in relation to other programming languages:  "What Forth is not"
  94. ----------------------------------------------------------------------
  95.  
  96.    Forth was meant as something more than a programming language.  In a 
  97.    traditional block-based Forth this is true.  It is somewhat less true 
  98.    in newer file-based Forths, however.  Still, Forth in any form has 
  99.    much to offer that distinguishes it from other languages.  Here are 
  100.    some comparisions:
  101.    
  102.    Forth is not an interpreter like BASIC, or Lisp, although it has an 
  103.    interpreter within it.
  104.    
  105.    Forth is not a fixed compiler like C or Pascal, although it uses a 
  106.    compiler.
  107.    
  108.    Forth is not an unstructured programming language like COBOL or older 
  109.    FORTRANs. (no GOTO, not even with a compiler switch :) , but one could
  110.    be added if you as programmer feel it is necessary)
  111.    
  112.    Forth is not a low-level language like assembly, but it contains 
  113.    low-level instructions and often contains an assembler.
  114.    
  115.    If one were forced to choose, Forth is somewhat like C, but far freer 
  116.    than C.  As one humor piece put it:
  117.    
  118.        The task:  Shoot yourself in the foot.
  119.        ---------------------------------------   
  120.        C: "You shoot yourself in the foot."
  121.        Forth: "Foot yourself shoot in."       (remember: Forth is postfix)
  122.        
  123.    
  124.    I think of Forth and C in this way:
  125.     
  126.            +---------------------------+
  127.            |                           |
  128.            | Forth  +---------------+  |  Forth can do what C does and
  129.            |        |               |  |  anything else that C doesn't
  130.            |        | C             |  |  do by extending the compiler.
  131.            |        |               |  |
  132.            |        |               |  |
  133.            |        +---------------+  |
  134.            |                           |
  135.            +---------------------------+
  136.            
  137.    (Note: Forth cannot do what C++ does :)
  138.     
  139.    While it is at about the same level as C, i.e. a mix of high-level 
  140.    and low-level, Forth has the advantages of a built in interpreter and a 
  141.    high level of extensibility.  If the C compiler does not have what 
  142.    you want, too bad.  If the Forth compiler does not have what you 
  143.    want, then simply extend the compiler with CREATE and DOES>, or even 
  144.    alter the nucleus (or kernel) and metacompile the system (metacompile 
  145.    is a Forth term for rebuilding the system.  Most Forths are written 
  146.    largely in Forth and can compile themselves.)
  147.    
  148.    In some views, Forth is less a programming language than it is a 
  149.    system for creating custom programming languages to suit the 
  150.    application at hand.
  151.    
  152.    Many conside